package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;

import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.dialog.attributevalueassignmentdialog.AttributeValueAssignmentDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnPropertyNode extends SaturnBaseComponentNode {

	private static final long serialVersionUID = 1L;
	private static final String SHOW_COMPONENT_VALUE = Messages.getString("SaturnPropertyNode.PropertyValueComponent"); //$NON-NLS-1$
	private static int number = 1;

	private Map<String, List<SaturnDbDataElement>> bigMap = new HashMap<String, List<SaturnDbDataElement>>();
	
	public void fire(){
		firePropertyChange(PROPERTERY_CHANGS, null, "");
	}
	
	public Dialog getDialog() {
		AttributeValueAssignmentDialog dialog = new AttributeValueAssignmentDialog(super.getShell(), this);
		return dialog;
	}

	public SaturnPropertyNode() {
		super.setName( SHOW_COMPONENT_VALUE + number++);
	}

	@Override
	public String getException() {
		return "";
	}

	@Override
	public String getType() {
		return "SaturnPropertyNode";
	}

	@Override
	public String getFullKey() {
		return super.fullKey;
	}

	public Map<String, List<SaturnDbDataElement>> getBigMap() {
		return bigMap;
	}

	public void setBigMap(Map<String, List<SaturnDbDataElement>> bigMap) {
		this.bigMap = bigMap;
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnPropertyNode saturnComponentNode = new SaturnPropertyNode();
		SaturnPropertyNode saturn = (SaturnPropertyNode) shape;
		saturnComponentNode.setLocation(mousePoint);
		saturnComponentNode.setKey(saturn.getKey());
		saturnComponentNode.setName(saturn.getName());
		saturnComponentNode.setDescription(saturn.getDescription());
		saturnComponentNode.setParameters(saturn.getParameters());
		saturnComponentNode.setException(saturn.getException());
		saturnComponentNode.setFullKey(saturn.getFullKey());
		saturnComponentNode.setId(saturn.getId());
		saturnComponentNode.setNextId(saturn.getNextId());
		return saturnComponentNode;
	}

	@Override
	protected void setImage(Image image) {
	}
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"saturndata"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "saturndata",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		Map<String, List<?>> summm = new HashMap<String, List<?>>();
		List<SaturnParameter> parameters = getParameters();
		List<Parameters> paras = new ArrayList<Parameters>();
		String name = getName();
		Parameters para = new Parameters();

		para.setName(getName());
		para.setId(getId());
		para.setKey(getKey());
		para.setParameterValueKey(getParameters().get(0)
				.getValueKey());
		para.setFullKey(getFullKey());
		para.setDescription(getDescription());

		String attributeName = "";
		for (int i = 1; i < parameters.size(); i++) {

			if (!(i % 2 == 0)) {
				attributeName = parameters.get(i).getValueKey().trim();
			} else {
				Parameters tempParameters = new Parameters();
				tempParameters.setAttributeValue(parameters.get(i).getValueKey());
				tempParameters.setAttributeName(attributeName.replaceAll("'",""));
				paras.add(tempParameters);
				attributeName = "";
			}
		}

		basicvaluse.add(para);

		para.setInfourl(name + "-propertyNode.html");
		para.setInfoname(name );
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.ParaValueNode")); //$NON-NLS-1$
		para.setImagename("propertyNode");
		summm.put("parameters", paras);
		summm.put("basicvaluse", basicvaluse);
		String templatePath = "template/export/logic/property";
		String Path = filePath + File.separator + name + "-propertyNode.html";
		FreemarkerUtils.writeTemplateToFile(templatePath, summm, Path);

		return para;
	}
}
